<?php

namespace app\api\controller;

class NoAuth extends Basic
{
    protected $postInfo = [];
    protected $user = [];
    protected $userid = 0;
    protected $table;
    protected $userInfo = [];

    public function __construct()
    {
        parent::__construct();
        $this->initUserinfo();
    }

    private function initUserinfo()
    {
        $info = $this->getUserInfoByToken();
        if (!empty($info)) {
            if ($info['table'] == 'member') {
                $where = ['memberid' => $info['userid']];
                $table = 'member';
                $this->role = 1;
            } else {
                $where = ['coachid' => $info['userid']];
                $table = 'coach';
                $this->role = 2;
            }

            $res = $this->findTable($table, ['where' => $where]);
            if ($info['table'] == 'member') {
                unset($res['memberpassword']);
                $username = $res['membertruename'];
            } else {
                unset($res['coachpassword']);
                $username = $res['coachtruename'];
            }
            $this->userid = $info['userid'];
            $this->user = $res;
            $this->table = $table;

            $active = $this->findTable('user_active', ['where' => [
                ['userid', '=', $this->userid],
                ['table', '=', $this->table],
                ['addtime', 'between', [strtotime(date('Y-m-d')), time()]],
            ], 'field' => 'id']);

            if(empty($active)) {
                $this->insertTable('user_active', [
                    'userid' => $this->userid,
                    'table' => $this->table,
                    'username' => $username,
                ]);
            }

            if (((time() - $info['addtime']) / 86400) > 15 || ($res['status'] == 4 && $info['table'] == 'member') || ($res['status'] == 5 && $info['table'] == 'coach')) {
                $update = [
                    'where' => [
                        'table' => $this->table,
                        'status' => 1,
                        'userid' => $this->userid,
                    ],
                    'data' => ['status' => 0],
                ];

                $this->updateTable('user_login', $update);
                $this->initreturn('登录超时，请重新登录');
            }
        } else {
            $this->initreturn('登录超时，请重新登录');
        }
    }

    protected function initreturn($msg)
    {
        $execTime = $this->microtime_float() - $this->begin;
        $result = [
            'code' => 999,
            'message' => $msg,
            'time' => $execTime ? $execTime : time(),
            'result' => [],
        ];
        exit(json_encode($result, JSON_UNESCAPED_UNICODE));
    }

      /**
     * 更新教练
     */
    public function updateDrivInfo()
    {
        $post = input('post.');

        $arr = ['school' => '驾校', 'coachprovinceid' => '省份', 'coachcityid' => '市级', 'coachaddressinfo' => '驾校地址', 'seniority' => '资历', 'license' => '驾驶证号码', 'faceidcard' => '身份证正面照', 'backidcard' => '身份证反面照', 'holdingimg' => '手持身份证图片', 'licenseimg' => '驾驶证图片'];

        $noarr = ['coachareaid' => '区级', 'addressimg' => '驾驶场地图片', 'drivingimg' => '行驶证图片', 'qualificationimg' => '资格证图片', 'postimg' => '上岗证图片', 'coachingimg' => '教练证图片', 'otherimg' => '其他证图片'];
        $in_array = array_merge(array_keys($arr), array_merge($noarr));

        $where = ['coachid' => $this->userid];
        $info = $this->findTable('coach', ['where' => $where]);
        if (empty($info)) {
            return $this->json($this->userid, 1000, '无效编辑');
        }

        if ($info['status'] == 1) {
            foreach ($arr as $key => $val) {
                if (empty($post[$key])) {
                    return $this->json($post, 1000, '请输入正确' . $val);
                }
            }
        }

        $data = [];
        foreach ($in_array as $val) {
            if (isset($post[$val])) {
                $data[$val] = empty($post[$val]) ? '' : $post[$val];
            }
        }

        if (empty($data)) {
            return $this->json($this->userid, 1000, '编辑成功');
        }

        if (!empty($post['longitude']) && !empty($post['latitude'])) {
            $data['longitude'] = $post['longitude'];
            $data['latitude'] = $post['latitude'];
        }

        if (empty($data['longitude']) || empty($data['latitude'])) {
            $address = $data['coachaddressinfo'];
            $result = $this->getgaode($address);
            if ($result['code'] == 1000) {
                if (!empty($result['data'])) {
                    $location = $result['data'];
                    $data['longitude'] = empty($location[0]) ? '' : $location[0];
                    $data['latitude'] = empty($location[1]) ? '' : $location[1];
                }
            } else {
                return $this->json($result['data'], $result['code'], $result['msg']);
            }
        }

        $data['status'] = 2;
        $where = ['coachid' => $this->userid];
        $update = [
            'where' => $where,
            'data' => $data,
        ];

        $ans = $this->updateTable('coach', $update);

        if ($ans) {
            return $this->json($ans, 1000);
        } else {
            return $this->json($ans, 2000);
        }
    }

}
